import { createStore } from "vuex";
import router from "@/router";

function addNewRouter(menuList) {

    let routers = router.options.routes

    routers.forEach(routeItem=>{
        if(routeItem.path=="/Index"){
            menuList.forEach(menu=>{
                let childRouter = {
                    path:'/'+menu.grouppath,
                    name:menu.groupname,
                    meta:{
                        title:menu.groupname
                    },
                    compontnent:()=>import('../components/Answer')
                }
                routeItem.children.push(childRouter);
            })
        }
    })
    console.log(routers)
    
}

export default createStore({
    state: {
        tota: localStorage.getItem("tota") ? localStorage.getItem("tota") : null,
        menu : [],
        value:{
            uid:''
        }

    },
    mutations: {
        setToTa(state, payload) {
            state.tota = payload;
            localStorage.setItem("tota", payload);
        },
        clearToTa() {
            localStorage.removeItem("tota");
        },
        setMenu(state, menuList) {

            this.state.menu = menuList
            addNewRouter(menuList)

        },
        setValue(state, value) {
            this.state.value = value
        }
    },
    actions: {
        SETTOTA(context, payload) {
            context.commit("setToTa", payload);
        },
        CLEARTOTA(context) {
            context.commit("clearToTa");
        },
    },
    modules: {},
    getters:{
        getMenu(state){
            return this.state.menu
        },
        getValue(state){
            return this.state.value
        }
    }
});